package cn.tedu.weibo.service.Impl;

import cn.tedu.weibo.mapper.WeiboMapper;
import cn.tedu.weibo.pojo.dto.WeiboDTO;
import cn.tedu.weibo.pojo.entity.Weibo;
import cn.tedu.weibo.pojo.vo.WeiboDetailVO;
import cn.tedu.weibo.pojo.vo.WeiboIndexVO;
import cn.tedu.weibo.service.IWeiboService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpSession;
import java.util.Collections;
import java.util.Date;
import java.util.List;

@Service
@Slf4j
public class WeiboServiceImpl implements IWeiboService {

    @Autowired
    private WeiboMapper weiboMapper;

    @Override
    public int insert(WeiboDTO weiboDTO, Long id) {
        Weibo weibo = new Weibo();
        weibo.setCreated(new Date());
        weibo.setUserId(id);
        BeanUtils.copyProperties(weiboDTO,weibo);
        int res = weiboMapper.insert(weibo);
        if(res != 1){
            log.debug("微博发布失败！");
            return 1;
        }
        return 1;
    }

    @Override
    public List<WeiboIndexVO> selectIndex() {
        List<WeiboIndexVO> list = weiboMapper.selectIndex();
        return list;
    }

    @Override
    public WeiboDetailVO selectById(Long id, String username) {
        WeiboDetailVO weiboDetailVO = weiboMapper.selectWeibo(id, username);
        return weiboDetailVO;
    }
}
